<?php
class EZoho extends CApplicationComponent {
	public $username;
	public $password;
	public $apiKey;

	public function createLeadFromCoffeeShop($coffeeShop,$source='MCR Website'){
		$lead=array();
		$name=$coffeeShop->ownerName?$coffeeShop->ownerName:$coffeeShop->email;
		$nameParts=split(' ',$name,2);

		$lead['Lead Owner']=$this->username;
		$lead['Company']=$coffeeShop->name;
		$lead['First Name']=$nameParts[0];
		$lead['Last Name']=isset($nameParts[1])?$nameParts[1]:'';
		$lead['Email']=$coffeeShop->email;
		$lead['Phone']=$coffeeShop->phone;
		$lead['Website']=$coffeeShop->website;
		$lead['Lead Source']=$source;
		$lead['Email Opt Out']=false;
		$lead['Street']=$coffeeShop->street;
		$lead['Suburb']=$coffeeShop->suburb;
		$lead['State']=$coffeeShop->state;
		$lead['Zip Code']=$coffeeShop->postcode;
		$lead['Country']=$coffeeShop->country;
		$lead['Description']=CHtml::link(
			'View Online: '.$coffeeShop->getVoteId(),
		Yii::app()->getController()->createAbsoluteUrl('coffeeShop/viewShop',array('id'=>$coffeeShop->id)),
		array('target'=>'_blank'));
		$lead['Description']=$lead['Description'];
		return $this->createLead($lead);

	}

	public function createLead($leadsParams){
		$leadsUrl='https://crm.zoho.com/crm/private/xml/Leads/insertRecords';
		$ticket= $this->generateTicket();

		$leadsData='<Leads><row no="1">';
		foreach($leadsParams as $key=>$value){
			$leadsData.='<FL val="'.$key.'">'.htmlspecialchars($value).'</FL>';
		}
		$leadsData.='</row></Leads>';
		$params='newFormat=1&version=2&apikey='.$this->apiKey.'&ticket='.$ticket.'&xmlData='.urlencode($leadsData);

		try{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $leadsUrl);
			curl_setopt($ch, CURLOPT_POST      ,1);
			curl_setopt($ch, CURLOPT_POSTFIELDS    ,$params);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
			curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
			curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL

			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
			
			$Rec_Data = curl_exec($ch);
			curl_close($ch);
			
			if(!$Rec_Data){
				return false;
			}else{
				return true;
			}

		}catch(Exception $e){
			return false;
		}




		return;
	}

	private function generateTicket(){
		$ticketUrl='https://accounts.zoho.com/login';
		$params='servicename=ZohoCRM&FROM_AGENT=true&LOGIN_ID='.urlencode($this->username).'&PASSWORD='.urlencode($this->password);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $ticketUrl);
		curl_setopt($ch, CURLOPT_POST      ,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS    ,$params);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION  ,1);
		curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
		curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

		$Rec_Data = curl_exec($ch);
		curl_close($ch);
		return $this->parseTicket($Rec_Data);
	}


	private function parseTicket($Rec_Data){
		$entries=split("\n",$Rec_Data);
		foreach($entries as $entry){
			$kv=split('=',$entry);
			if($kv[0]=='TICKET'){
				return $kv[1];
			}
		}
		return false;
	}
}